package com.pshcp.service;

import com.framework.util.SystemUtil;
import com.pshcp.domain.dto.PatientOnlineMedicalRecordsDTO;
import com.pshcp.domain.model.PatientOnlineMedicalRecords;
import com.pshcp.mapper.PatientOnlineMedicalRecordsMapper;

import com.github.pagehelper.PageHelper;
import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.pshcp.common.enums.CommonStatusEnum;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 患者在线订单关联病历 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2022-11-02
 */
@Service
public class PatientOnlineMedicalRecordsService {

    @Autowired
    private PatientOnlineMedicalRecordsMapper patientOnlineMedicalRecordsMapper;

    public PageList<PatientOnlineMedicalRecords> list(PatientOnlineMedicalRecordsDTO patientOnlineMedicalRecordsDTO){
        if(patientOnlineMedicalRecordsDTO.getPageNum() != null && patientOnlineMedicalRecordsDTO.getPageSize() != null){
            PageHelper.startPage(patientOnlineMedicalRecordsDTO.getPageNum(), patientOnlineMedicalRecordsDTO.getPageSize());
        }
        Example example = new Example(PatientOnlineMedicalRecords.class);
        Example.Criteria criteria = example.createCriteria();
        if(patientOnlineMedicalRecordsDTO.getStatus() != null){
            criteria.andEqualTo("status", patientOnlineMedicalRecordsDTO.getStatus());
        }
        if(patientOnlineMedicalRecordsDTO.getPatientUserId() != null){
            criteria.andEqualTo("patientUserId", patientOnlineMedicalRecordsDTO.getPatientUserId());
        }
        if(patientOnlineMedicalRecordsDTO.getStartTime() != null){
            criteria.andGreaterThanOrEqualTo("createdTime", patientOnlineMedicalRecordsDTO.getStartTime());
        }
        if(patientOnlineMedicalRecordsDTO.getEndTime() != null){
            criteria.andLessThanOrEqualTo("createdTime", patientOnlineMedicalRecordsDTO.getEndTime());
        }
        example.setOrderByClause(" created_time desc");
        List<PatientOnlineMedicalRecords> list = patientOnlineMedicalRecordsMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<PatientOnlineMedicalRecords> select(PatientOnlineMedicalRecords patientOnlineMedicalRecords){
        return patientOnlineMedicalRecordsMapper.select(patientOnlineMedicalRecords);
    }

    public int insert(PatientOnlineMedicalRecords patientOnlineMedicalRecords){
        patientOnlineMedicalRecords.setId(SystemUtil.getId());
        patientOnlineMedicalRecords.setStatus(CommonStatusEnum.INIT.getCode());
        patientOnlineMedicalRecords.setCreatedTime(new Date());
        return patientOnlineMedicalRecordsMapper.insert(patientOnlineMedicalRecords);
    }

    public int update(PatientOnlineMedicalRecords patientOnlineMedicalRecords){
        patientOnlineMedicalRecords.setUpdateTime(new Date());
        return patientOnlineMedicalRecordsMapper.updateByPrimaryKeySelective(patientOnlineMedicalRecords);
    }

    public PatientOnlineMedicalRecords findById(Long id){
        return patientOnlineMedicalRecordsMapper.selectByPrimaryKey(id);
    }

    public PatientOnlineMedicalRecords findByOnlineOrderId(Long patientOnlineOrderId){
        Example example = new Example(PatientOnlineMedicalRecords.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("patientOnlineOrderId", patientOnlineOrderId);
        List<PatientOnlineMedicalRecords> records = patientOnlineMedicalRecordsMapper.selectByExample(example);
        return CollectionUtils.isEmpty(records) ? null : records.get(0);
    }

    public List<PatientOnlineMedicalRecords> findByOnlineOrderIds(List<Long> ids){
        Example example = new Example(PatientOnlineMedicalRecords.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("patientOnlineOrderId", ids);
        return patientOnlineMedicalRecordsMapper.selectByExample(example);
    }

    public List<PatientOnlineMedicalRecords> findByIds(List<Long> ids){
        Example example = new Example(PatientOnlineMedicalRecords.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return patientOnlineMedicalRecordsMapper.selectByExample(example);
    }

    public int delById(Long id){
        Example example = new Example(PatientOnlineMedicalRecords.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        PatientOnlineMedicalRecords patientOnlineMedicalRecords = new PatientOnlineMedicalRecords();
        patientOnlineMedicalRecords.setStatus(CommonStatusEnum.DEL.getCode());
        return patientOnlineMedicalRecordsMapper.updateByExampleSelective(patientOnlineMedicalRecords, example);
    }

}
